package com.smedi.ismedi.database.starter.aspect;

import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.database.starter.constant.DataBaseConstant;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: xulinjun
 * @Date: 2021-7-8 13:43
 */
@Aspect
@Slf4j
@Component
@Lazy(value = false)
@Order(value = 2)
@Deprecated
public class SqlPermissionAspect {
//    private HttpServletRequest getRequest() {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        return request;
//    }
//
//    @Pointcut("@annotation(com.smedi.ismedi.database.starter.annotation.SkipSqlPermission)")
//    private void cutExecute() {
//    }
//
//    /***
//     * 全过程
//     * @param pjp
//     * @return
//     */
//    @Around(value = "cutExecute()&&@annotation(skipSqlPermission)")
//    public Object doAround(ProceedingJoinPoint pjp, SkipSqlPermission skipSqlPermission) throws Throwable {
//        log.info("SkipSqlPermission 开始执行方法 >>> class:{}, method:{}", pjp.getSignature().getClass().getName(), pjp.getSignature().getName());
//        Object[] params = pjp.getArgs();
//        for (Object param : params) {
//            log.info(" + 入参: {}", param);
//        }
//        return pjp.proceed(); // 执行代码
//    }
//
//    @Before(value = "cutExecute()&&@annotation(skipSqlPermission)")
//    public void doBefore(JoinPoint joinPoint, SkipSqlPermission skipSqlPermission) {
//        log.info("添加跳过数据权限验证");
//        HttpServletRequest request = getRequest();
//        request.getSession().setAttribute(DataBaseConstant.skipSqlPermission, true);
//    }
//
//    @After(value = "cutExecute()&&@annotation(skipSqlPermission)")
//    public void doAfter(JoinPoint joinPoint, SkipSqlPermission skipSqlPermission) {
//        log.info("删除跳过数据权限验证");
//        HttpServletRequest request = getRequest();
//        request.getSession().removeAttribute(DataBaseConstant.skipSqlPermission);
//    }
}